home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / 3DGPL 1.0 / CODE / HARDWARE / WIN-32 / HARDWARE.C next >
Encoding:
C/C++ Source or Header  |  1995-06-22  |  6.9 KB  |  223 lines  |  [TEXT/MACA]

  1. /** 3DGPL *************************************************\
  2.  *  (MS-WINDOWS, 286+, 8bit deep modes)                   *
  3.  *  Header for hardware specific stuff.                   *
  4.  *                                                        *
  5.  *  Defines:                                              *
  6.  *    HW_open_screen         opening output surface;      *
  7.  *    HW_blit                colourmap onto the screen;   *
  8.  *    HW_close_screen        closing output;              *
  9.  *                                                        *
  10.  *    HW_run_event_loop      runiing for events;          *
  11.  *    HW_quit_event_loop     quiting running.             *
  12.  *                                                        *
  13.  *  (6/1995) By Sergei Savhenko. (savs@cs.mcgill.ca).     *
  14.  *  Copyright (c) 1995 Sergei Savchenko.                  *
  15.  *  THIS SOURCE CODE CAN'T BE USED FOR COMERCIAL PURPOSES *
  16.  *  WITHOUT AUTHORISATION                                 *
  17. \**********************************************************/
  18.  
  19. #include <windows.h>                        /* all API stuff */
  20. #include "../hardware/hardware.h"           /* macros */
  21.  
  22. extern void main(void);                     /* real main (well...) */
  23.  
  24. HDC HW_mem;                                 /* why? i wish i know why... */
  25. HBITMAP HW_bmp;                             /* bitmap header */
  26. RECT HW_rect;                               /* "client" area dimensions */
  27. HWND HW_wnd;                                /* window */
  28. HANDLE HW_instance;                         /* instance of this thing */
  29. HPALETTE HW_palette;                        /* the palette headers */
  30.  
  31. int HW_cmd_show;                            /* not sure but needed */
  32. unsigned char *HW_colourmap;                /* the bitmap bits */
  33. char HW_class_name[]="hardware";            /* what is class name anyway? */
  34.  
  35. struct                                      /* the logical palette */
  36. {
  37.  WORD hw_version;
  38.  WORD hw_no_entries;
  39.  PALETTEENTRY hw_entries[256];
  40. } HW_log_palette;
  41.  
  42. void HW_null_function(void) {}              /* default handler */
  43. void (*HW_application_main)(void)=HW_null_function;
  44. void (*HW_application_key_handler)(int key_code);
  45.  
  46. /**********************************************************\
  47.  *  Implementations for fast memory operations.           *
  48. \**********************************************************/
  49.  
  50. void HW_set_int(int *d,long l,int v) {long i; for(i=0;i<l;i++) *d++=v; }
  51.  
  52. /**********************************************************\
  53.  *  Creating a window.                                    *
  54. \**********************************************************/
  55.  
  56. int HW_open_screen(char *display_name,
  57.             char *screen_name,
  58.            struct HW_colour palette[256],
  59.            unsigned char *colourmap
  60.           )
  61. {
  62.  PAINTSTRUCT ps;                            
  63.  int i;
  64.   
  65.  HW_wnd=CreateWindow(HW_class_name,screen_name,WS_SYSMENU,
  66.              CW_USEDEFAULT,CW_USEDEFAULT,
  67.              HW_SCREEN_X_SIZE,HW_SCREEN_Y_SIZE+29,
  68.              NULL,NULL,HW_instance,NULL);
  69.  
  70.  HW_colourmap=colourmap;
  71.  HW_mem=CreateCompatibleDC(BeginPaint(HW_wnd,&ps));
  72.  if((GetDeviceCaps(ps.hdc,PLANES)!=1)&&
  73.      (GetDeviceCaps(ps.hdc,PLANES)!=8))
  74.   MessageBox(NULL,"Screen mode with 256 colours needed...",
  75.           screen_name,MB_OK|MB_ICONSTOP);
  76.  
  77.  HW_bmp=CreateCompatibleBitmap(ps.hdc,HW_SCREEN_X_SIZE,HW_SCREEN_Y_SIZE);
  78.  SelectObject(HW_mem,HW_bmp);
  79.  
  80.  HW_log_palette.hw_version=0x300;
  81.  HW_log_palette.hw_no_entries=256;
  82.  for(i=0;i<256;i++)
  83.  {
  84.   HW_log_palette.hw_entries[i].peRed=palette[i].hw_r;
  85.   HW_log_palette.hw_entries[i].peGreen=palette[i].hw_g;
  86.   HW_log_palette.hw_entries[i].peBlue=palette[i].hw_b;
  87.   HW_log_palette.hw_entries[i].peFlags=PC_NOCOLLAPSE;
  88.  }
  89.  HW_palette=CreatePalette((LOGPALETTE*)&HW_log_palette);
  90.  EndPaint(HW_wnd,&ps);
  91.  
  92.  HW_rect.left=HW_rect.top=0;
  93.  HW_rect.right=HW_SCREEN_X_SIZE;
  94.  HW_rect.bottom=HW_SCREEN_Y_SIZE;
  95.  
  96.  ShowWindow(HW_wnd,HW_cmd_show);
  97.  UpdateWindow(HW_wnd);
  98.  
  99.  return(1);
  100. }
  101.  
  102. /**********************************************************\
  103.  *  Rendering the bitmap into the window.                 *
  104. \**********************************************************/
  105.  
  106. void HW_blit(void)
  107. {
  108.  PAINTSTRUCT ps;
  109.  
  110.  BeginPaint(HW_wnd,&ps);
  111.  
  112.  SelectPalette(ps.hdc,HW_palette,FALSE);
  113.  RealizePalette(ps.hdc);
  114.  SetMapMode(ps.hdc,MM_TEXT);
  115.  SetBitmapBits(HW_bmp,HW_COLOURMAP_SIZE_CHAR,HW_colourmap);
  116.  BitBlt(ps.hdc,0,0,HW_SCREEN_X_SIZE,HW_SCREEN_Y_SIZE,HW_mem,0,0,SRCCOPY);
  117.  
  118.  EndPaint(HW_wnd,&ps);
  119. }
  120.  
  121. /**********************************************************\
  122.  *  Deallocating some stuff.                              *
  123. \**********************************************************/
  124.  
  125. void HW_close_screen(void)
  126. {
  127.  DeleteDC(HW_mem);
  128.  DeleteObject(HW_bmp);
  129. }
  130.  
  131. /**********************************************************\
  132.  *  Running the event loop.                               *
  133. \**********************************************************/
  134.  
  135. void HW_run_event_loop(void (*application_main)(void),
  136.                void (*application_key_handler)(int key_code)
  137.               )
  138. {
  139.  MSG msg;
  140.  
  141.  HW_application_main=application_main;
  142.  HW_application_key_handler=application_key_handler;
  143.  
  144.  while(1)
  145.  {
  146.   if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))  /* this IS sensitive part! */ 
  147.   {
  148.    if(msg.message == WM_QUIT) break;
  149.    TranslateMessage(&msg);                  
  150.    DispatchMessage(&msg);  
  151.   }
  152.   else
  153.   {
  154.    InvalidateRect(HW_wnd,&HW_rect,TRUE);
  155.    UpdateWindow(HW_wnd);
  156.   }
  157.  }
  158. }
  159.  
  160. /**********************************************************\
  161.  *  Running the event loop.                               *
  162. \**********************************************************/
  163.  
  164. void HW_quit_event_loop(void)
  165. {
  166.  PostMessage(HW_wnd,WM_CLOSE,0,0L);         /* telling ourselves to quit */
  167. }
  168.  
  169. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  170.  *  Windows main callback function.                      * 
  171. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  172.  
  173. long FAR PASCAL WndProc(HWND hWnd,UINT message,
  174.             WPARAM wParam,LPARAM lParam)
  175. {
  176.  switch(message)
  177.  {  
  178.   case WM_PAINT:       HW_application_main();
  179.                break;   
  180.   case WM_ERASEBKGND:  return(1L);
  181.   case WM_DESTROY:     PostQuitMessage(0);
  182.                break;
  183.   case WM_KEYDOWN:     HW_application_key_handler(wParam);
  184.                break;
  185.   default: return(DefWindowProc(hWnd,message,wParam,lParam));
  186.  }
  187.  return(0L);
  188. }
  189.  
  190. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  191.  *  Windows Main function.                               *
  192. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  193.  
  194. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  195.            LPSTR lpszCmdLine,int nCmdShow)
  196. {
  197.  WNDCLASS w;
  198.  
  199.  HW_cmd_show=nCmdShow;
  200.  if(!(HW_instance=hPrevInstance))
  201.  {
  202.   w.style=CS_HREDRAW|CS_VREDRAW;
  203.   w.lpfnWndProc=(LPVOID)WndProc;
  204.   w.cbClsExtra=0;
  205.   w.cbWndExtra=0;
  206.   w.hInstance=hInstance;
  207.   w.hIcon=NULL;
  208.   w.hCursor=NULL;
  209.   w.hbrBackground=GetStockObject(WHITE_BRUSH);
  210.   w.lpszMenuName=NULL;
  211.   w.lpszClassName=HW_class_name;
  212.  
  213.   if(! RegisterClass(&w))
  214.    return FALSE;
  215.  }
  216.  
  217.  main();                                    /* here, human way to start */
  218.  
  219.  return(0);
  220. }
  221.  
  222. /**********************************************************/
  223.